package com.tbsoft.ehr.service.overtime;

import java.math.BigDecimal;
import java.util.List;

import com.tbsoft.ehr.dao.overtime.OvertimeDao;
import com.tbsoft.ehr.dao.staffFile.StaffFileDao;
import com.tbsoft.ehr.model.StaffInfo;
import com.tbsoft.ehr.model.WorkOvertime;

public class OvertimeServiceImpl implements OvertimeService {

	private OvertimeDao overtimeDao;
	private StaffFileDao staffFileDao;
	
	public OvertimeDao getOvertimeDao() {
		return overtimeDao;
	}

	public void setOvertimeDao(OvertimeDao overtimeDao) {
		this.overtimeDao = overtimeDao;
	}

	public StaffFileDao getStaffFileDao() {
		return staffFileDao;
	}

	public void setStaffFileDao(StaffFileDao staffFileDao) {
		this.staffFileDao = staffFileDao;
	}

	@Override
	public void addOvertime(WorkOvertime overtime) {
		overtimeDao.saveOrUpdate(overtime);
	}

	@Override
	public WorkOvertime queryOvertime(WorkOvertime overtime) {
		return overtimeDao.getOvertime(overtime);
	}

	@Override
	public BigDecimal getOvertimesByStaffId(String staffId) {
		BigDecimal sum = new BigDecimal("0");
		List<WorkOvertime> overtimes = overtimeDao.getOvertimesByStaffId(staffId);
		for(WorkOvertime ot:overtimes) {
			sum = sum.add(ot.getHours());			
		}
		return sum;
	}

	@Override
	public List<WorkOvertime> getOvertimeByCondton(String staffName,
			String yearMonth, int startIndex, int count) {
		List<StaffInfo> staffLst = null;
		if(staffName!=null&&!staffName.isEmpty()){
			staffLst = staffFileDao.getStaffFileByStaffName(staffName);	
			//输入条件没有取到数据，直接返回
			if(staffLst.size()==0){
				return null;
			}
		}
		return overtimeDao.getOvertimeByCondton(staffLst, yearMonth, startIndex, count);
	}

	@Override
	public int getOvertimeCount(String staffName, String yearMonth) {
		List<StaffInfo> staffLst = null;
		
		if(staffName!=null&&!staffName.isEmpty()){
			staffLst = staffFileDao.getStaffFileByStaffName(staffName);		
			//该用户名没有取到数据
			if(staffLst.size()==0){
				return 0;
			}
		}
		return overtimeDao.getOvertimeCount(staffLst, yearMonth);
	}

	@Override
	public WorkOvertime getOvertimeById(int id) {
		return overtimeDao.getOvertimeById(id);
	}

}
